package com.capgemini.wechat.service;

import com.capgemini.commons.JsonUtil;
import com.capgemini.remind.service.CommonsService;
import com.capgemini.wechat.dto.PushCouponInfo;
import com.capgemini.wechat.dto.PushSaleOrderInfo;
import com.capgemini.wechat.dto.PushServiceRemindInfo;
import com.capgemini.wechat.mapper.WechatPushMapper;
import com.capgemini.wechat.utils.WechatPostUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author
 * @date
 */
@Service
public class WechatPushService {
	public final Logger log = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private WechatPushMapper wechatPushMapper;
	@Autowired
	private CommonsService commonsService;

	/**
	 * 推送优惠券信息
	 */
	public void pushCoupon() {String wechatPushServiceHost = commonsService.getCodeValueByCodeTypeAndCode("wechatConfig", "pushWechatHost");
        if(StringUtils.isBlank(wechatPushServiceHost)){
            log.error("wechat server is not configured.");

            return;
        }

        List<PushCouponInfo> couponInfos = wechatPushMapper.getPushCoupons();

		log.debug("push coupon data: {}", JsonUtil.getJson(couponInfos));
		String result = null;
		if(!couponInfos.isEmpty()){
//			new WechatPostUtil().pushToWechatByUrl("http://crmwechatuat.chinacloudsites.cn/tyreplus/api/sendGiftCertificateExpireNotice", couponInfos);
			result = new WechatPostUtil().pushToWechatByUrl(wechatPushServiceHost + "/tyreplus/api/sendGiftCertificateExpireNotice", couponInfos);
		}

		//   log
		Date now = new Date();

		boolean successFlag = false;
		if(StringUtils.isNotBlank("result")){
			try {
				Map<String, String> map = JsonUtil.fromJson(result, Map.class);
				successFlag = StringUtils.equalsIgnoreCase(String.valueOf(map.get("status")), "1");
			} catch (IOException e) {
				log.error("", e);
			}
		}

		for (PushCouponInfo pushServiceRemind : couponInfos) {
			pushServiceRemind.setSuccessFlag(successFlag);
			pushServiceRemind.setPushTime(now);
			pushServiceRemind.setPushResult(result);
		}

		int index = 0;
		List<PushCouponInfo> dataList = new ArrayList<PushCouponInfo>();
		for (PushCouponInfo pushServiceRemind : couponInfos) {
			dataList.add(pushServiceRemind);
			index ++;
			if(index % 100 == 0){
				wechatPushMapper.batchInsertCouponLog(dataList);
				dataList.clear();
				index = 0;
			}
		}

		if(index > 0){
			wechatPushMapper.batchInsertCouponLog(dataList);
		}

	}



	/**
	 * 推送保养提醒信息
	 */
	public void pushServiceRemind() {String wechatPushServiceHost = commonsService.getCodeValueByCodeTypeAndCode("wechatConfig", "pushWechatHost");
        if(StringUtils.isBlank(wechatPushServiceHost)){
            log.error("wechat server is not configured.");

            return;
        }

        List<PushServiceRemindInfo> pushServiceReminds = wechatPushMapper.getPushServiceReminds();

		log.debug("push serviceRemind data: {}", JsonUtil.getJson(pushServiceReminds));
		String result = null;
		if(!pushServiceReminds.isEmpty()){
//			new WechatPostUtil().pushToWechatByUrl("http://crmwechatuat.chinacloudsites.cn/tyreplus/api/sendMaintenanceExpireNotice", pushServiceReminds);
			result = new WechatPostUtil().pushToWechatByUrl(wechatPushServiceHost + "/tyreplus/api/sendMaintenanceExpireNotice", pushServiceReminds);
		}

		// log
		Date now = new Date();

		boolean successFlag = false;
		if(StringUtils.isNotBlank("result")){
			try {
				Map<String, String> map = JsonUtil.fromJson(result, Map.class);
				successFlag = StringUtils.equalsIgnoreCase(String.valueOf(map.get("status")), "1");
			} catch (IOException e) {
				log.error("", e);
			}
		}

		for (PushServiceRemindInfo pushServiceRemind : pushServiceReminds) {
			pushServiceRemind.setSuccessFlag(successFlag);
			pushServiceRemind.setPushTime(now);
			pushServiceRemind.setPushResult(result);
		}

		int index = 0;
		List<PushServiceRemindInfo> dataList = new ArrayList<PushServiceRemindInfo>();
		for (PushServiceRemindInfo pushServiceRemind : pushServiceReminds) {
			dataList.add(pushServiceRemind);
			index ++;
			if(index % 100 == 0){
				wechatPushMapper.batchInsertRemindLog(dataList);
				dataList.clear();
				index = 0;
			}
		}

		if(index > 0){
			wechatPushMapper.batchInsertRemindLog(dataList);
		}

	}

	/**
	 * 推送订单信息
	 */
	public void pushSaleOrder() {
        String wechatPushServiceHost = commonsService.getCodeValueByCodeTypeAndCode("wechatConfig", "pushWechatHost");
        if(StringUtils.isBlank(wechatPushServiceHost)){
            log.error("wechat server is not configured.");

            return;
        }

		List<PushSaleOrderInfo> pushSaleOrderInfoList = wechatPushMapper.getPushSaleOrder();

		log.debug("push serviceRemind data: {}", JsonUtil.getJson(pushSaleOrderInfoList));

		String result = null;
		if(!pushSaleOrderInfoList.isEmpty()){
//			new WechatPostUtil().pushToWechatByUrl("http://crmwechatuat.chinacloudsites.cn/tyreplus/api/sendConsumeNotice", pushSaleOrderInfoList);
//			result = new WechatPostUtil().pushToWechatByUrl("http://crmwechat.chinacloudsites.cn/tyreplus/api/sendConsumeNotice", pushSaleOrderInfoList);
			result = new WechatPostUtil().pushToWechatByUrl(wechatPushServiceHost + "/tyreplus/api/sendConsumeNotice", pushSaleOrderInfoList);
		}

		//  2016/7/26  log
		Date now = new Date();

		boolean successFlag = false;
		if(StringUtils.isNotBlank("result")){
			try {
				Map<String, String> map = JsonUtil.fromJson(result, Map.class);
				successFlag = StringUtils.equalsIgnoreCase(String.valueOf(map.get("status")), "1");
			} catch (IOException e) {
				log.error("", e);
			}
		}

		for (PushSaleOrderInfo pushServiceRemind : pushSaleOrderInfoList) {
			pushServiceRemind.setSuccessFlag(successFlag);
			pushServiceRemind.setPushTime(now);
			pushServiceRemind.setPushResult(result);
			pushServiceRemind.setPushType("ORDER");
		}

		int index = 0;
		List<PushSaleOrderInfo> dataList = new ArrayList<PushSaleOrderInfo>();
		for (PushSaleOrderInfo pushServiceRemind : pushSaleOrderInfoList) {
			dataList.add(pushServiceRemind);
			index ++;
			if(index % 100 == 0){
				wechatPushMapper.batchInsertOrderLog(dataList);
				dataList.clear();
				index = 0;
			}
		}

		if(index > 0){
			wechatPushMapper.batchInsertOrderLog(dataList);
		}

	}

}
